<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="HTML中为button绑定事件的方式有三种。 例如以下标签： &amp;lt;button type=&amp;quot;submit&amp;quot; id=&amp;quot;btn_submit&amp;quot;&amp;gt; submit &amp;lt;/button&amp;gt; 使用jquery进行绑定 123$(&amp;apos;#btn_submit&amp;apos;).click(function()&amp;#123;&amp;#125;); 使用原生js">
<meta property="og:type" content="article">
<meta property="og:title" content="button绑定事件的三种方式">
<meta property="og:url" content="http://yoursite.com/2018/06/06/button绑定事件的三种方式/index.html">
<meta property="og:site_name" content="From&#39;s blog">
<meta property="og:description" content="HTML中为button绑定事件的方式有三种。 例如以下标签： &amp;lt;button type=&amp;quot;submit&amp;quot; id=&amp;quot;btn_submit&amp;quot;&amp;gt; submit &amp;lt;/button&amp;gt; 使用jquery进行绑定 123$(&amp;apos;#btn_submit&amp;apos;).click(function()&amp;#123;&amp;#125;); 使用原生js">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-06-06T13:12:11.454Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="button绑定事件的三种方式">
<meta name="twitter:description" content="HTML中为button绑定事件的方式有三种。 例如以下标签： &amp;lt;button type=&amp;quot;submit&amp;quot; id=&amp;quot;btn_submit&amp;quot;&amp;gt; submit &amp;lt;/button&amp;gt; 使用jquery进行绑定 123$(&amp;apos;#btn_submit&amp;apos;).click(function()&amp;#123;&amp;#125;); 使用原生js">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/06/06/button绑定事件的三种方式/"/>





  <title>button绑定事件的三种方式 | From's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">From's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/06/06/button绑定事件的三种方式/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Wang hang">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="From's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">button绑定事件的三种方式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-06-06T21:11:30+08:00">
                2018-06-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h5 id="HTML中为button绑定事件的方式有三种。"><a href="#HTML中为button绑定事件的方式有三种。" class="headerlink" title="HTML中为button绑定事件的方式有三种。"></a>HTML中为button绑定事件的方式有三种。</h5><hr>
<p>例如以下标签：</p>
<p><code>&lt;button type=&quot;submit&quot; id=&quot;btn_submit&quot;&gt; submit &lt;/button&gt;</code></p>
<h3 id="使用jquery进行绑定"><a href="#使用jquery进行绑定" class="headerlink" title="使用jquery进行绑定"></a>使用jquery进行绑定</h3><hr>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$(&apos;#btn_submit&apos;).click(function()&#123;</span><br><span class="line"></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="使用原生js绑定，"><a href="#使用原生js绑定，" class="headerlink" title="使用原生js绑定，"></a>使用原生js绑定，</h3><hr>
<p>注意：Internet Explorer 8 及更早IE版本不支持 addEventListener() 方法，Opera 7.0 及 Opera 更早版本也不支持。 这类浏览器版本要使用 attachEvent() 方法来添加事件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">document.getElementById(&quot;#btn_submit&quot;).addEventListener(‘click’, function()&#123;</span><br><span class="line"></span><br><span class="line">&#125;, false);</span><br></pre></td></tr></table></figure></p>
<p>补充：addEventListener的第三个参数是用于决定事件模型的，当父元素和子元素都绑定了事件时，这个参数决定先触发哪个事件，false为冒泡事件模型：即子元素绑定的事件先响应，父元素绑定的事件后相应，true问捕获事件模型，与冒泡事件模型执行顺序相反，如：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;div id=&quot;test_div&quot;&gt;</span><br><span class="line">   &lt;button type=&quot;button&quot; value =&quot;测试事件顺序&quot; name=&quot;测试事件顺序&quot; id=&quot;test_button&quot;&gt;测试事件顺序&lt;/button&gt;</span><br><span class="line">&lt;/div&gt; </span><br><span class="line"></span><br><span class="line">document.getElementById(&apos;test_div&apos;).addEventListener(&apos;click&apos;, function () &#123;</span><br><span class="line">    console.log(&apos;div&apos;);</span><br><span class="line">&#125;,true)</span><br><span class="line">document.getElementById(&apos;test_button&apos;).addEventListener(&apos;click&apos;, function()&#123;</span><br><span class="line">    console.log(&apos;test1&apos;);</span><br><span class="line">&#125;,false);</span><br></pre></td></tr></table></figure></p>
<p>这个例子的事件模型是捕获模型，会先执行div的事件再执行button的事件，这里有个需要注意的地方：决定事件模型的是父元素绑定事件时传的第三个参数，如上例中button绑定事件时传的第三个参数是不起作用的，除非它又包含了子元素。</p>
<p>对事件模型不了解的可以查看改链接：<a href="http://www.cnblogs.com/dtdxrk/archive/2012/06/28/2567132.html" target="_blank" rel="noopener">http://www.cnblogs.com/dtdxrk/archive/2012/06/28/2567132.html</a></p>
<h3 id="直接在button标签中使用onclick绑定"><a href="#直接在button标签中使用onclick绑定" class="headerlink" title="直接在button标签中使用onclick绑定"></a>直接在button标签中使用onclick绑定</h3><hr>
<p><code>&lt;button type=&quot;submit&quot; id=&quot;btn_submit&quot; onclick=&quot;btnAction()&quot;&gt; submit &lt;/button&gt;</code></p>
<p>然后在<script>标签中定义btnAtion的方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">        function btnAction()</span><br><span class="line">        &#123; &#125; </span><br><span class="line"></span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure></p>
<h4 id="比较："><a href="#比较：" class="headerlink" title="比较："></a>比较：</h4><p>1、使用jquery绑定，代码简洁，使用方便，事件绑定方式为追加绑定，即绑定多少个方法就执行多少个方法。</p>
<p>在单一绑定的条件下，由于jQuery底层其实也是js实现，所以速度区别并不大。至少“绑定”这个环节并不会成为</p>
<p>速度的瓶颈，除非页面上绑定事件的元素超过上万个，否则响应速度就不必纠结了，只做个事件绑定还是很快的。</p>
<p>所以在做负载等要求不那么严格的“小程序”，从维护的角度上，建议用jQuery绑定，简单清楚，最容易维护。</p>
<p>2、使用原生js与jquery相比的话代码量稍大，但是能让人进一步了解事件绑定的细节，对于熟悉原生js的开发者来说是值得推荐的。</p>
<p>3、使用onclick标签绑定，代码量不大，但是html前端和js前端混在一起，不易于维护。</p>
<p>原则上HTML代码只能体现网页的结构，具体的行为应该使用javascript代码进行绑定。</p>
<p>如果在HTML中用onclick事件混杂js，会导致html前端和js前端的工作混在了一起，难以分离工作任务，</p>
<p>进而难以维护。这种做法临时调试可以，但如果正式成品中不应该出现，</p>
<p>所以不建议使用onclick标签方式进行绑定事件。</p>
</script></p>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/05/27/了解Cookie/" rel="next" title="了解Cookie">
                <i class="fa fa-chevron-left"></i> 了解Cookie
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/06/10/安装webpack及配置/" rel="prev" title="安装webpack及配置">
                安装webpack及配置 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Wang hang</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">52</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-5"><a class="nav-link" href="#HTML中为button绑定事件的方式有三种。"><span class="nav-number">1.</span> <span class="nav-text">HTML中为button绑定事件的方式有三种。</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用jquery进行绑定"><span class="nav-number"></span> <span class="nav-text">使用jquery进行绑定</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用原生js绑定，"><span class="nav-number"></span> <span class="nav-text">使用原生js绑定，</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#直接在button标签中使用onclick绑定"><span class="nav-number"></span> <span class="nav-text">直接在button标签中使用onclick绑定</span></a></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Wang hang</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
